package org.mybatis.generator.plugins;


import java.util.List;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;

public class AeyeQueryListPlugin extends PluginAdapter{
	private String sqlMapId = "queryList";
	private String parameterType = "com.aeye.mbr.common.base.DataMap";

	public boolean validate(List<String> warnings) {
		return true;
	}

	@Override
	public boolean clientGenerated(Interface interfaze,
			TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		return super.clientGenerated(interfaze, topLevelClass, introspectedTable);
	}

	@Override
	public boolean sqlMapDocumentGenerated(Document document,
			IntrospectedTable introspectedTable) {
		 XmlElement answer = new XmlElement("select"); //$NON-NLS-1$

	        answer.addAttribute(new Attribute(
	                "id", sqlMapId)); //$NON-NLS-1$
	        if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
	            answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
	                    introspectedTable.getResultMapWithBLOBsId()));
	        } else {
	            answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
	                    introspectedTable.getBaseResultMapId()));
	        }


	        answer.addAttribute(new Attribute("parameterType", //$NON-NLS-1$
	                parameterType));

	        context.getCommentGenerator().addComment(answer);

	        StringBuilder sb = new StringBuilder();
	        sb.append("select * from "); //$NON-NLS-1$

//	        if (stringHasValue(introspectedTable
//	                .getSelectByPrimaryKeyQueryId())) {
//	            sb.append('\'');
//	            sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
//	            sb.append("' as QUERYID,"); //$NON-NLS-1$
//	        }
//	        answer.addElement(new TextElement(sb.toString()));
//	        answer.addElement(getBaseColumnListElement());
//	        if (introspectedTable.hasBLOBColumns()) {
//	            answer.addElement(new TextElement(",")); //$NON-NLS-1$
//	            answer.addElement(getBlobColumnListElement());
//	        }
	        sb.append(introspectedTable
	                .getAliasedFullyQualifiedTableNameAtRuntime());
	        answer.addElement(new TextElement(sb.toString()));

	        XmlElement trimElement = new XmlElement("trim");
	        trimElement.addAttribute(new Attribute("prefix", "where"));
	        trimElement.addAttribute(new Attribute("prefixOverrides", "and |or"));
	        answer.addElement(trimElement);
	        for (IntrospectedColumn introspectedColumn : introspectedTable
	                .getAllColumns()) {
	           
	        	 XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
	             sb.setLength(0);
	             sb.append(introspectedColumn.getJavaProperty());
	             sb.append(" != null "); //$NON-NLS-1$
//	             sb.append(introspectedColumn.getJavaProperty());
//	             sb.append(" != ''");
	             isNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$
	             trimElement.addElement(isNotNullElement);

	             sb.setLength(0);
	             sb.append(" and ");
	             sb.append(MyBatis3FormattingUtilities
	                     .getEscapedColumnName(introspectedColumn));
	             sb.append(" = "); //$NON-NLS-1$
	             sb.append(MyBatis3FormattingUtilities
	                     .getParameterClause(introspectedColumn));

	             isNotNullElement.addElement(new TextElement(sb.toString()));
	        }
	        //add orderBy cluase
	        XmlElement orderByElement = new XmlElement("if"); 
            sb.setLength(0);
            sb.append("orderBy != null");
            orderByElement.addAttribute(new Attribute("test", sb.toString()));
            sb.setLength(0);
            sb.append(" order by ${orderBy}");
            orderByElement.addElement(new TextElement(sb.toString()));
            answer.addElement(orderByElement);
	        document.getRootElement().addElement(answer);
	        return true;
	}
	

}
